package net.expectx.pay.gateway.validator;

import com.baidu.unbiz.fluentvalidator.Validator;
import com.baidu.unbiz.fluentvalidator.ValidatorContext;
import com.baidu.unbiz.fluentvalidator.ValidatorHandler;
import com.google.common.base.Strings;

/**
 * 校验不为null
 * Created by shuzheng on 2017/2/18.
 */
public class LengthValidator extends ValidatorHandler<String> implements Validator<String> {

    private Integer min=0;
    private Integer max=2147483647;

    public LengthValidator(Integer min,Integer max) {
        this.min = min;
        this.max = max;
    }

    @Override
    public boolean validate(ValidatorContext context, String checkedString) {
        if (!Strings.isNullOrEmpty(checkedString) && (checkedString.length()<min || checkedString.length() > max)){
            context.addErrorMsg(String.format("字符串长度不正确,invalid value=%s",checkedString));
            return false;
        }
        return true;
    }

}
